<?php

if (!defined('IN_IA')) {
    die( 'Access Denied' );
}

class express{

    public $tableExpress = 'ewei_shop_plugin_express';
    public $tableDispatch = 'ewei_shop_dispatch';
    public $tableExpressCourier = 'ewei_shop_plugin_express_courier';


    /**
     * 获取配送列表
     * @return array
     */
    public function getDispatchLists(){
        global $_W;

        $sql  = ' SELECT * FROM '.tablename('ewei_shop_dispatch');
        $sql .= ' WHERE `uniacid` = :uniacid AND `dispatchtype` = 0 AND `enabled` = 1 ';

        return pdo_fetchall($sql, array(':uniacid' => $_W['uniacid']));
    }

    /**
     * 获取配送详情
     * @return array
     */
    public function getDispatch($dispatchid){
        global $_W;

        $sql  = ' SELECT `dispatchname`, `express` FROM '.tablename('ewei_shop_dispatch');
        $sql .= ' WHERE `uniacid` = :uniacid AND `id` = :dispatchid AND `enabled` = 1 ';

        return pdo_fetch($sql, array(':uniacid' => $_W['uniacid'], ':dispatchid' => $dispatchid));
    }

    /**
     * 获取所有快递预处理列表
     * @return array
     */
    public function getExpressLists(){
        global $_W;

        $sql  = ' SELECT e.*, d.dispatchname FROM '.tablename($this->tableExpress).' AS e ';
        $sql .= ' LEFT JOIN '.tablename($this->tableDispatch).' AS d ON e.dispatchid = d.id ';
        $sql .= ' WHERE e.`uniacid` = :uniacid';

        return pdo_fetchall($sql, array(':uniacid' => $_W['uniacid']));
    }

    /**
     * 根据快递ID，查询单号池列表
     * @param $dispatchid
     * @return bool
     */
    public function getExpressIdByDispatchid($dispatchid){
        global $_W;

        $sql  = ' SELECT `id` FROM '.tablename($this->tableExpress).' WHERE `uniacid` = :uniacid AND `dispatchid` = :dispatchid ';

        return pdo_fetchcolumn($sql, array(':uniacid' => $_W['uniacid'], ':dispatchid' => $dispatchid));
    }

    /**
     * 根据快递ID，查询单号池列表
     * @param $dispatchid
     * @return bool
     */
    public function getExpressDetailByDispatchid($dispatchid){
        global $_W;

        $sql  = ' SELECT * FROM '.tablename($this->tableExpress).' WHERE `uniacid` = :uniacid AND `dispatchid` = :dispatchid ';

        return pdo_fetch($sql, array(':uniacid' => $_W['uniacid'], ':dispatchid' => $dispatchid));
    }

    /**
     * 获取单个快递预处理设置
     * @param $expressId
     * @return bool
     */
    public function getExpressDetail($expressId){
        global $_W;

        if(empty($expressId)){
            return false;
        }

        $sql  = ' SELECT * FROM '.tablename($this->tableExpress);
        $sql .= ' WHERE `id` = :expressId AND `uniacid` = :uniacid LIMIT 1';

        return pdo_fetch($sql, array(':expressId' => $expressId, ':uniacid' => $_W['uniacid']));
    }

    /**
     * 获取快递模板
     * @param $expressName
     * @return bool | array
     */
    public function getTemplate($expressName){

        if(empty($expressName)){
            $method = '_defalutTemplate';
        }else{
            $method = '_'.$expressName.'Template';
        }

        if(method_exists($this, $method)){
            return $this->$method();
        }

        return false;
    }

    /**
     * 默认快递
     * @return array
     */
    private function _defalutTemplate(){
//        $columns = array(
//            array('title' => '订单编号', 'field' => 'ordersn', 'width' => 24),
//            array('title' => '物流运单号', 'field' => 'expresssn', 'width' => 24),
//            array('title' => '发件姓名', 'field' => 'sender', 'width' => 12),
//            array('title' => '发件手机', 'field' => 'phone', 'width' => 12),
//            array('title' => '发件省', 'field' => 'senderprovince', 'width' => 12),
//            array('title' => '发件详细地址', 'field' => 'senderaddress', 'width' => 24),
//            array('title' => '收件人姓名', 'field' => 'realname', 'width' => 12),
//            array('title' => '收件人手机', 'field' => 'mobile', 'width' => 16),
//            array('title' => '收件人省', 'field' => 'province', 'width' => 12),
//            array('title' => '收件人市', 'field' => 'city', 'width' => 12),
//            array('title' => '收件人所在区县', 'field' => 'area', 'width' => 12),
//            array('title' => '收件人详细地址', 'field' => 'address', 'width' => 24),
//            array('title' => '订单物品', 'field' => 'goods_title', 'width' => 24),
//            array('title' => '数量', 'field' => 'total', 'width' => 12),
//        );

        $columns = array(
            array('title' => '物流订单号', 'field' => 'ordersn', 'width' => 24),
            array('title' => '物流运单号', 'field' => 'expresssn', 'width' => 24),
            array('title' => '发件姓名', 'field' => 'sender', 'width' => 12),
            array('title' => '发件手机', 'field' => 'phone', 'width' => 12),
            array('title' => '发件省', 'field' => 'senderprovince', 'width' => 12),
            array('title' => '发件市', 'field' => 'sendercity', 'width' => 12),
            array('title' => '发件区县', 'field' => 'senderdistrict', 'width' => 12),
            array('title' => '发件详细地址', 'field' => 'senderaddress', 'width' => 24),
            array('title' => '收件人姓名', 'field' => 'realname', 'width' => 12),
            array('title' => '收件人手机', 'field' => 'mobile', 'width' => 12),
            array('title' => '收件人省', 'field' => 'province', 'width' => 12),
            array('title' => '收件人市', 'field' => 'city', 'width' => 12),
            array('title' => '收件人所在区县', 'field' => 'area', 'width' => 12),
            array('title' => '收件人详细地址', 'field' => 'address', 'width' => 24),
            array('title' => '商品总价', 'field' => 'goodsprice', 'width' => 24),
            array('title' => '商品名称', 'field' => 'goods_title', 'width' => 24),
            array('title' => '商品数量', 'field' => 'total', 'width' => 12),
            array('title' => '商品单价', 'field' => 'goodspicprice', 'width' => 12),
            array('title' => '商品重量(克)', 'field' => 'goodsweight', 'width' => 12),
            array('title' => '商品类型', 'field' => 'goodstype', 'width' => 12),
            array('title' => '备注', 'field' => 'remark', 'width' => 12),

        );

        return $columns;
    }

    /**
     * 国通快递模板
     * @return array
     */
    private function _guotongkuaidiTemplate(){

        $columns = array(
            array('title' => '订单编号', 'field' => 'ordersn', 'width' => 30),
            array('title' => '物流运单号', 'field' => 'expresssn', 'width' => 24),
            array('title' => '发件姓名', 'field' => 'sender', 'width' => 12),
            array('title' => '发件手机', 'field' => 'phone', 'width' => 16),
            array('title' => '发件省', 'field' => 'senderprovince', 'width' => 12),
            array('title' => '发件详细地址', 'field' => 'senderaddress', 'width' => 30),
            array('title' => '收件人姓名', 'field' => 'realname', 'width' => 12),
            array('title' => '收件人手机', 'field' => 'mobile', 'width' => 16),
            array('title' => '收件人省', 'field' => 'province', 'width' => 12),
            array('title' => '收件人市', 'field' => 'city', 'width' => 12),
            array('title' => '收件人所在区县', 'field' => 'area', 'width' => 12),
            array('title' => '收件人详细地址', 'field' => 'address', 'width' => 30),
            array('title' => '订单物品', 'field' => 'goods_title', 'width' => 30),
            array('title' => '数量', 'field' => 'total', 'width' => 6),
        );

        return $columns;
    }

    /**
     * 国通快递模板
     * @return array
     */
    private function _shentongTemplate(){

        $columns = array(
            array('title' => '订单编号', 'field' => 'ordersn', 'width' => 24),
            array('title' => '物流运单号', 'field' => 'expresssn', 'width' => 24),
            array('title' => '发件姓名', 'field' => 'sender', 'width' => 12),
            array('title' => '发件手机', 'field' => 'phone', 'width' => 12),
            array('title' => '发件省', 'field' => 'senderprovince', 'width' => 12),
            array('title' => '发件详细地址', 'field' => 'senderaddress', 'width' => 24),
            array('title' => '收件人姓名', 'field' => 'realname', 'width' => 12),
            array('title' => '收件人手机', 'field' => 'mobile', 'width' => 12),
            array('title' => '收件人省', 'field' => 'province', 'width' => 12),
            array('title' => '收件人市', 'field' => 'city', 'width' => 12),
            array('title' => '收件人所在区县', 'field' => 'area', 'width' => 12),
            array('title' => '收件人详细地址', 'field' => 'address', 'width' => 24),
            array('title' => '订单物品', 'field' => 'goods_title', 'width' => 24),
            array('title' => '数量', 'field' => 'total', 'width' => 12),
        );

        return $columns;
    }


    /**
     * 百世快递模板
     * @return array
     */
    private function _baishiTemplate(){

        $columns = array(
            array('title' => '物流订单号', 'field' => 'ordersn', 'width' => 24),
            array('title' => '物流运单号', 'field' => 'expresssn', 'width' => 24),
            array('title' => '发件姓名', 'field' => 'sender', 'width' => 12),
            array('title' => '发件手机', 'field' => 'phone', 'width' => 12),
            array('title' => '发件省', 'field' => 'senderprovince', 'width' => 12),
            array('title' => '发件市', 'field' => 'sendercity', 'width' => 12),
            array('title' => '发件区县', 'field' => 'senderdistrict', 'width' => 12),
            array('title' => '发件详细地址', 'field' => 'senderaddress', 'width' => 24),
            array('title' => '收件人姓名', 'field' => 'realname', 'width' => 12),
            array('title' => '收件人手机', 'field' => 'mobile', 'width' => 12),
            array('title' => '收件人省', 'field' => 'province', 'width' => 12),
            array('title' => '收件人市', 'field' => 'city', 'width' => 12),
            array('title' => '收件人所在区县', 'field' => 'area', 'width' => 12),
            array('title' => '收件人详细地址', 'field' => 'address', 'width' => 24),
            array('title' => '商品总价', 'field' => 'goodsprice', 'width' => 24),
            array('title' => '商品名称', 'field' => 'goods_title', 'width' => 24),
            array('title' => '商品数量', 'field' => 'total', 'width' => 12),
            array('title' => '商品单价', 'field' => 'goodspicprice', 'width' => 12),
            array('title' => '商品重量(克)', 'field' => 'goodsweight', 'width' => 12),
            array('title' => '商品类型', 'field' => 'goodstype', 'width' => 12),
            array('title' => '备注', 'field' => 'remark', 'width' => 12),

        );

        return $columns;
    }

}